package com.xomily.devops.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.xomily.common.annotation.Log;
import com.xomily.common.core.controller.BaseController;
import com.xomily.common.core.domain.AjaxResult;
import com.xomily.common.enums.BusinessType;
import com.xomily.devops.domain.DevCpuLog;
import com.xomily.devops.service.IDevCpuLogService;

import cn.hutool.json.JSONUtil;

import com.xomily.common.utils.StringUtils;
import com.xomily.common.utils.DateUtils;
import com.xomily.common.utils.poi.ExcelUtil;
import com.xomily.common.core.page.TableDataInfo;

/**
 * CPU监测日志Controller
 * 
 * @author xomily
 * @date 2021-11-29
 */
@RestController
@RequestMapping("/devops/cpuLog")
public class DevCpuLogController extends BaseController
{
    @Autowired
    private IDevCpuLogService devCpuLogService;

    /**
     * 查询CPU监测日志列表
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(DevCpuLog devCpuLog)
    {
        startPage();
        List<DevCpuLog> list = devCpuLogService.selectDevCpuLogList(devCpuLog);
        return getDataTable(list);
    }
    
    
    /**
     * 查询CPU监测日志图表分析
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:graph')")
    @GetMapping("/graph")
    public AjaxResult graph(@RequestParam(name = "id") long id,@RequestParam(name ="dt",required = false) Date dt)
    {   	
    	Map<String, Object> params = new HashMap<String, Object>();

        if ( id ==0) {
        	   return AjaxResult.error("Id is error!");
        }
        Date t =DateUtils.getNowDate(); 
        if (dt!=null) {
        	t = dt;
        };
        
        String graphDate = DateUtils.dateTime(t);
        params.put("startTime", DateUtils.parseDate(graphDate + " 00:00:00"));
        params.put("endTime", DateUtils.parseDate(graphDate + " 23:59:59"));
        params.put("hostId", String.valueOf(id));
        Map<String, Object> result = devCpuLogService.selectDevCpuLogByGraph(params);
        return AjaxResult.success(result);
    }
    
    
    /**
     * 导出CPU监测日志列表
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:export')")
    @Log(title = "CPU监测日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DevCpuLog devCpuLog)
    {
        List<DevCpuLog> list = devCpuLogService.selectDevCpuLogList(devCpuLog);
        ExcelUtil<DevCpuLog> util = new ExcelUtil<DevCpuLog>(DevCpuLog.class);
        return util.exportExcel(list, "CPU监测日志数据");
    }

    /**
     * 获取CPU监测日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(devCpuLogService.selectDevCpuLogById(id));
    }

    /**
     * 新增CPU监测日志
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:add')")
    @Log(title = "CPU监测日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DevCpuLog devCpuLog)
    {
        return toAjax(devCpuLogService.insertDevCpuLog(devCpuLog));
    }

    /**
     * 修改CPU监测日志
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:edit')")
    @Log(title = "CPU监测日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DevCpuLog devCpuLog)
    {
        return toAjax(devCpuLogService.updateDevCpuLog(devCpuLog));
    }

    /**
     * 删除CPU监测日志
     */
    @PreAuthorize("@ss.hasPermi('devops:cpuLog:remove')")
    @Log(title = "CPU监测日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(devCpuLogService.deleteDevCpuLogByIds(ids));
    }
}
